import { Script } from '../entity'
import { v4 as uuidv4 } from 'uuid'

export async function getProjectScripts(projectId: string): Promise<Script[]> {
  return await window.db.all<Script>(
    `SELECT * FROM project_scripts WHERE project_id = ?`,
    [projectId]
  )
}

export async function addScript(projectId: string, script: Omit<Script, 'id'>): Promise<string> {
  const id = uuidv4()
  await window.db.run(
    `INSERT INTO project_scripts (id, project_id, name, description, content, command,scriptType,filename)
     VALUES (?, ?, ?, ?, ?, ?,?,?)`,
    [id, projectId, script.name, script.description, script.content, script.command,script.scriptType,script.filename]
  )
  return id
}

export async function updateScript(script: Script): Promise<void> {
  await window.db.run(
    `UPDATE project_scripts 
     SET name = ?, description = ?, content = ?, command = ?
     WHERE id = ?`,
    [script.name, script.description, script.content, script.command, script.id]
  )
}

export async function removeScript(id: string): Promise<void> {
  await window.db.run('DELETE FROM project_scripts WHERE id = ?', [id])
}

// ... 其他脚本相关的方法 